package com.w.utils.version;
/**
 *
 **/

import com.w.utils.string.StringUtils;

/**
 * @Author weiwei
 * @Date 2021-03-05 17:28
 * @description 版本工具类
 **/
public class VersionUtils {

    /**
     * 比较当前版本是否大于等于目标版本,关于坑点可以查看think的string.md
     * @param currentVersion
     * @param targetVersion
     * @return
     */
    private static int comportToVersion(String currentVersion,String targetVersion){
        if(StringUtils.isEmpty(currentVersion) ||  StringUtils.isEmpty(targetVersion)){
            return -1;
        }
        String[] current = currentVersion.split("\\.");
        String[] target = targetVersion.split("\\.");
        int idx = 0;
        int minLength = Math.min(current.length, target.length);//取最小长度值
        int diff = 0;
        while (idx < minLength
                && (diff = current[idx].length() - target[idx].length()) == 0//先比较长度
                && (diff = current[idx].compareTo(target[idx])) == 0) {//再比较字符
            ++idx;
        }
        //如果已经分出大小，则直接返回，如果未分出大小，则再比较位数，有子版本的为大；
        diff = (diff != 0) ? diff : current.length - target.length;
        return diff;
    }
}
